/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace TFrameWork.Module.ActionEditor
{
    internal partial class ActionEditor
    {
        public Action<MouseMoveEvent> MouseMoveEvent;
        public Action<MouseUpEvent> MouseUpEvent;
        public Action<MouseOutEvent> MouseOutEvent;

        private VisualElement trackMenuParent;
        private VisualElement contentListView;
        private ScrollView mainContentView;
        private IMGUIContainer TrackBackGround;

        private VisualElement CreateDataBg;
        private Button CreateDataButton;
        
        /// <summary>
        /// 所有的轨道
        /// </summary>
        private List<TrackBase> trackList = new List<TrackBase>();

        private void InitContent()
        {
            contentListView = _root.Q<VisualElement>("ContentListView");
            trackMenuParent = _root.Q<VisualElement>("TrackMenuList");
            mainContentView = _root.Q<ScrollView>("MainContentView");
            TrackBackGround = _root.Q<IMGUIContainer>(nameof(TrackBackGround));
            TrackBackGround.onGUIHandler = DrawTrackBackGround;
            CreateDataBg = _root.Q(nameof(CreateDataBg));
            CreateDataButton = _root.Q<Button>(nameof(CreateDataButton));
            CreateDataButton.clicked += CreateNewActionData;
            
            TrackBackGround.RegisterCallback<MouseUpEvent>(TrackMouseUp);
            TrackBackGround.RegisterCallback<MouseMoveEvent>(TrackMouseMove);
            TrackBackGround.RegisterCallback<MouseOutEvent>(TrackMouseOut);
            
            mainContentView.verticalScroller.valueChanged += ContentVerticalScorllerValueChanged;
            UpdateContentSize();
            InitTrack();
        }

        private void TrackMouseOut(MouseOutEvent evt)
        {
            MouseOutEvent?.Invoke(evt);
        }

        private void TrackMouseUp(MouseUpEvent evt)
        {
            MouseUpEvent?.Invoke(evt);
        }

        private void TrackMouseMove(MouseMoveEvent evt)
        {
            MouseMoveEvent?.Invoke(evt);
        }

        private void DrawTrackBackGround()
        {
            Handles.BeginGUI();
            {
                Rect rect = TrackBackGround.contentRect;

                // 起始索引  (内容区域的偏移 / 当前的帧宽）
                int index = 0;
                // 计算绘制起点的偏移
                float startOffset = 0;
                //计算绘制数字文本的间隔
                int tickStep = ActionEditor.Instance.actionEditorConfig.maxFrameWidthLV + 1 -
                               (_actionEditorConfig.frameUnitWidth / ActionEditor.Instance.actionEditorConfig.standFrameUnitWidth);
                tickStep = tickStep / 2; // 可能 1 / 2 = 0的情况
                if (tickStep == 0) tickStep = 1; // 避免为0
                int width = (int)rect.width;
                if (_actionData != null)
                {
                    width = (_actionData.FrameCount + 1) * _actionEditorConfig.frameUnitWidth;
                }
                for (float i = startOffset; i < width; i += _actionEditorConfig.frameUnitWidth)
                {
                    // 绘制长线、文本
                    if (index % tickStep == 0)
                    {
                        Handles.color = new Color(1, 1, 1, 0.25f);
                        Handles.DrawLine(new Vector3(i, 0), new Vector3(i, rect.height));
                    }
                    else
                    {
                        Handles.color = new Color(1, 1, 1, 0.1f);
                        Handles.DrawLine(new Vector3(i, 0), new Vector3(i, rect.height));
                    }

                    index += 1;
                }
            }
            Handles.EndGUI();
        }

        private void ContentVerticalScorllerValueChanged(float value)
        {
            Vector3 pos = trackMenuParent.transform.position;
            pos.y = _contentContainer.transform.position.y;
            trackMenuParent.transform.position = pos;
        }

        /// <summary>
        /// 初始化轨道
        /// </summary>
        private void InitTrack()
        {
            // 如果没有配置，也不需要初始化轨道
            if (_actionData == null) return;
            InitAnimationTrack();
            InitAudioTrack();
            InitParticleTrack();
            InitTriggerTrack();
            InitSignalTrack();
            // ....
        }



        private void InitAnimationTrack()
        {
            //todo 新的动画轨道
            AnimationTrack track = new AnimationTrack();
            track.Init(trackMenuParent, contentListView);
            trackList.Add(track);
        }
        
        private void InitAudioTrack()
        {
            AudioTrack track = new AudioTrack();
            track.Init(trackMenuParent, contentListView);
            trackList.Add(track);
        }

        private void InitParticleTrack()
        {
            GameObjectTrack track = new GameObjectTrack();
            track.Init(trackMenuParent, contentListView);
            trackList.Add(track);
        }

        private void InitTriggerTrack()
        {
            TriggerTrack track = new TriggerTrack();
            track.Init(trackMenuParent, contentListView);
            trackList.Add(track);
        }

        /// <summary>
        /// 初始化信号轨道
        /// </summary>
        private void InitSignalTrack()
        {
            SignalTrack signalTrack = new SignalTrack();
            signalTrack.Init(trackMenuParent, contentListView);
            trackList.Add(signalTrack);
        }

        /// <summary>
        /// 重新绘制轨道内容
        /// </summary>
        private void ResetLoadTrack()
        {
            // 如果配置文件是Null，清理掉所有的轨道
            if (_actionData == null)
            {
                DestoryTracks();
            }
            else
            {
                // 如果轨道列表里面没有数据，说明没有轨道，当时当前用户是有配置的，需要初始化轨道
                if (trackList.Count == 0)
                {
                    InitTrack();
                }

                // 更新视图
                for (int i = 0; i < trackList.Count; i++)
                {
                    trackList[i].ResetLoadTrack();
                }
            }
        }

        private void DestoryTracks()
        {
            for (int i = 0; i < trackList.Count; i++)
            {
                trackList[i].Destroy();
            }

            trackList.Clear();
        }

        /// <summary>
        /// 更新轨道内容的大小
        /// </summary>
        private void UpdateContentSize()
        {
            contentListView.style.width = _actionEditorConfig.frameUnitWidth * CurrentFrameCount + 100;
        }

    }
}
